package com.apigateway.controller;

import com.apigateway.proxy.NettyProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 小泽玛丽罗 on 2017/2/24.
 */
@RestController
public class ProxyStartController {
    private static boolean proxyStarted=false;
    private static Logger logger= LoggerFactory.getLogger(ProxyStartController.class);
    @Value("${proxy.port}")
    private int port;
    @Value("${proxy.route}")
    private String route;

    @RequestMapping(value = "/startProxy",method = RequestMethod.GET)
    public String startProxy(){
        NettyProxy nettyProxy=new NettyProxy();
        try {
            logger.info("starting proxy port:"+port);
            if(!proxyStarted)
                nettyProxy.start(port,route);
            else
                return "proxy at "+port+" is started ";
        } catch (InterruptedException e) {
            e.printStackTrace();
            logger.error("starting proxy error port:"+port +e.getMessage(),e);
            return "false"+e.getMessage();
        }
        return "success";
    }

}
